import type { Plugin } from '@nuxt/types'
import { MutationsType, ActionsType } from '@/actions/user'
import { ActionsType as CityActionsType } from '@/actions/city'
import { getToken } from '@/utils/token'
const fn: Plugin = async function (params) {
  console.log('client.ts loaded!')
  const { store, route } = params
  const token = getToken()
  store.commit(MutationsType.SET_TOKEN, token)

  if (route.path.includes('admin')) {
    try {
      await store.dispatch(ActionsType.getUserInfo)
      await store.dispatch(ActionsType.setBaseInfo)
      if (route.path.includes('investmentMap')) {
        store.commit(MutationsType.SET_SHOW_MORE_APP, true)
      }
    } catch (error) {
      console.error(error)
    }
  } else {
    await store.dispatch(CityActionsType.getCity)
  }
}

export default fn
